package com.itheima.ai.config;

import com.itheima.ai.advisor.SensitiveWordAdvisor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfiguration {

    @Bean
    public ChatMemory chatMemory() {
        return new InMemoryChatMemory();  //记忆存储，默认
    }

    @Bean
    public SensitiveWordAdvisor sensitiveWordAdvisor() {
        return new SensitiveWordAdvisor();
    }

    @Bean
    public ChatClient chatClient(OllamaChatModel model, ChatMemory chatMemory) {
        return ChatClient
                .builder(model)
                .defaultSystem("你是一个热心、可爱的智能助手，你的名字叫小颜，请以小颜的身份和语气回答问题。")
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),          // 1. 日志顾问（记录请求响应）
                        new SensitiveWordAdvisor(),               // 2. 敏感词顾问（新增，优先检测违禁词）
                        new MessageChatMemoryAdvisor(chatMemory) // 3. 聊天记忆顾问（记录对话上下文）
                )
                .build();
    }
}
